package com.onemore.compare;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.voovan.http.server.HttpRequest;
import org.voovan.http.server.HttpResponse;
import org.voovan.tools.log.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.onemore.task.OneConfig;
import com.onemore.task.model.ModelB2CErpWms;
import com.onemore.task.model.ModelGeneral;
import com.onemore.task.model.ModelOneErp2Wms;
import com.onemore.task.model.ModelOneWms2Erp;
import com.onemore.task.model.ModelSpjhd;

public class CompareB2cErpWms  extends CompareBase{
	public static void process(HttpRequest req, HttpResponse resp) throws SQLException, InterruptedException
	{	
		String filterDate = "";
		String cangku = "'09001','09002'";
		String ycdfilter = "'1018','2001'";
		String starDate = req.getParameter("starDate");
		String endDate = req.getParameter("endDate");
		Properties properties= OneConfig.getProperties();
		if(properties != null) {
			String str = OneConfig.getProperties().getProperty("cangku");
			if(str != null) {
				cangku = str;
			}

			if(starDate == null) {
				str = OneConfig.getProperties().getProperty("starDate");
				if(str != null) {
					starDate = str;
				}
			}
			str = OneConfig.getProperties().getProperty("ycdfilter");
			if(str != null) {
				ycdfilter = str;
			}
		}
		if(starDate != null && starDate.length() > 0) {
			filterDate = " billdate>='"+starDate+"' AND";
		}else {
			starDate = "2018";
		}
		if(endDate != null && endDate.length() > 0) {
			filterDate += " billdate<='"+endDate+"' AND";
		}
		
		List<Map<String, Object>> listSpjhd = new ArrayList<Map<String, Object>>();
		String sql = req.getParameter("sql");
		/*if(sql == null)
			sql = "SELECT wms.BillId, DJBH, MAX(billdate) AS CDate, MAX(YDJH) AS YDJH, sum(QTYIN) AS QTYIN, SUM (SL) AS SL FROM ONE_WMS wms LEFT JOIN (select DJBH,YDJH,RQ,SL from SPJHD where DM2 IN ("+cangku+") AND RQ>='"+starDate+"' union all select DJBH,YDJH,RQ,SL from SPTHD where DM2 IN ("+cangku+") AND RQ>='"+starDate+"' union all select DJBH,YDJH,RQ,SL from PFXHD where DM2 IN ("+cangku+") AND RQ>='"+starDate+"' union all select DJBH,YDJH,RQ,SL from PFTHD where DM2 IN ("+cangku+") AND RQ>='"+starDate+"' union all select DJBH,YDJH,RQ,SL from SDPHD where DM2 IN ("+cangku+") AND RQ>='"+starDate+"' union all select YDJH AS DJBH,DJBH AS YDJH,RQ,SL from SDTHD where DM2 IN ("+cangku+") AND RQ>='"+starDate+"' union all select DJBH,YDJH,RQ,SL from QDDBD where DM2 IN ("+cangku+") AND RQ>='"+starDate+"' union all select YDJH AS DJBH,DJBH AS YDJH,RQ,SL from QDTHD where DM2 IN ("+cangku+") AND RQ>='"+starDate+"' union all select DJBH,YDJH,RQ,SL from CKTZD where DM2 IN ("+cangku+") AND RQ>='"+starDate+"' union all select DJBH,YDJH,RQ,SL from SPYCD where (DM2 IN ("+cangku+") OR DM1 IN ("+cangku+")) AND RQ>='"+starDate+"'  UNION ALL select DJBH,YDJH,RQ,SL_2 as SL from YSEND where RQ>='"+starDate+"' AND DM1 IN ("+cangku+") AND NOT (dm1 IN ("+ycdfilter+") AND dm2 IN ("+ycdfilter+"))) erp ON erp.YDJH = wms.BillId WHERE "+filterDate+" ( erp.DJBH IS NULL OR erp.SL != wms.QTYIN OR erp.YDJH IS NULL ) GROUP BY wms.BillId, DJBH";
		Logger.debug("Wms2Erp:"+sql);
		listSpjhd= ModelGeneral.getCompareList(sql);*/
		listSpjhd = ModelB2CErpWms.getList(starDate,endDate);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		jsonArray.addAll(listSpjhd);
		jsonObject.put("data", jsonArray);
		String str = jsonObject.toJSONString();
		resp.write(str);
	}
	
	public static String getString() throws SQLException, InterruptedException
	{	
		List<Map<String, Object>> listSpjhd = ModelSpjhd.getCompare();
		return listSpjhd.toString();
	}
}
